	var canDrg=false,thiso=null;
	var bdg_offsetX;
	var bdg_offsetY;
	var bdg_eventX;
	var bdg_eventY;
	var bdg_nearindex;
	var bdg_lastnearindex="-1";
	var bdg_curmodid;
    var curId=0;
var debug="";
function startDrag(o,e)
	{
	  if(canDrg==true)
	  {
	    return;
	  }
        	document.onmousedown=function()	
		{
			return false
		};
	
		document.body.onselectstart=function()
		{
			return false
		};

		while(!(o.nodeType==1&&o.getAttribute("rel")&&o.getAttribute("rel")=="drag")&&o.parentNode)
		{
			o=o.parentNode
		}
      		e=e?e:(window.event?window.event:null);
		canDrg=true;
		bdg_curmodid=o.id; 
		engage(o,e);
		thiso=document.getElementById("fordrag");
               	if(document.all)
		{
		  	thiso.setCapture();
		}
		thiso.style.display="";
		document.getElementById("dragbak").innerHTML=o.innerHTML;
		thiso.innerHTML=o.innerHTML;
		thiso.style.left=parseInt(e.clientX+document.body.scrollLeft-bdg_offsetX)+"px";
		thiso.style.top=parseInt(e.clientY+document.body.scrollTop-bdg_offsetY)+"px";
		thiso.style.width=o.offsetWidth;
                getall();
	}
	function engage(o3,evt)
	{
		evt=(evt)?evt:event;
		var t=o3.offsetTop;
		var l=o3.offsetLeft;
		while(o3=o3.offsetParent)
		{
			t+=o3.offsetTop;
			l+=o3.offsetLeft;
		}

		bdg_offsetX=evt.clientX+document.body.scrollLeft-l;
		bdg_offsetY=evt.clientY+document.body.scrollTop-t;

	};
	document.onmousemove=function(e)
	{ 
		if(canDrg)
			{
				e=e?e:(window.event?window.event:null);
				bdg_eventX=e.clientX+document.body.scrollLeft;
				bdg_eventY=e.clientY+document.body.scrollTop;
				thiso.style.left=(bdg_eventX-bdg_offsetX)+"px";
				thiso.style.top=(bdg_eventY-bdg_offsetY)+"px";

				bdg_lastnearindex=bdg_nearindex;
                              
				bdg_nearindex=getmin(e);
                               

				e.returnValue=false;

				if(bdg_nearindex!=bdg_lastnearindex)
				{
				    if(objt[bdg_lastnearindex])
					 {
					     var lastNearElem=document.getElementById(objt[bdg_lastnearindex].name);
						 if(lastNearElem.getAttribute('belong')=='left')
						 { 
						    selectElement(objt[bdg_lastnearindex].name,false);
						 }
					 }
					if(objt[bdg_nearindex].name==bdg_curmodid)
					{
						return;
					}
					
					var destElem=document.getElementById(objt[bdg_nearindex].name);
					if(destElem.getAttribute('belong')=='left')
					{
					    selectElement(objt[bdg_nearindex].name,true);
					
					}else
					{ 
						var srcElement=document.getElementById(bdg_curmodid);
						if(srcElement.getAttribute('belong')=='left')
						{
						   setTakeplace(bdg_curmodid);
						   addnewmod(bdg_curmodid,objt[bdg_nearindex].name);
					 					    
						}else
						{  
						   delmod(bdg_curmodid);
						   addnewmod(bdg_curmodid,objt[bdg_nearindex].name);
						}
					}
				
				}
			}
	}
function setTakeplace(id)
{
   var destElem=document.getElementById(id);
   curId++;
   destElem.setAttribute('id',curId);
   destElem.setAttribute('rel','drag');
   destElem.setAttribute('belong','left');
   destElem.setAttribute('takeplace','true');
   destElem.setAttribute('style','background:#C4FA91;');
   destElem.innerHTML="&nbsp;";
}
function replaceItem(srcElem,destElem)
{
   if(srcElem.getAttribute('belong')=="left")
   {
        var srcP=srcElem.parentNode;
		var destP=destElem.parentNode;
		
		srcP.appendChild(destElem);
		destP.appendChild(srcElem);
		
   }else if(destElem.getAttribute("takeplace")=="true")
   {
        delmod(srcElem.id);
		var destP=destElem.parentNode;
		destP.removeChild(destElem);
		srcElem.setAttribute('belong','left');
		destP.appendChild(srcElem);
   }else //
   {
        var srcP=srcElem.parentNode;
		var destP=destElem.parentNode;
		
		srcElem.setAttribute('belong','left');
		destElem.setAttribute('belong','right');

		destElem.setAttribute('style','');//?

		srcP.appendChild(destElem);
		destP.appendChild(srcElem);
   }
}
function selectElement(elemId,selected)
{
   var elem=document.getElementById(elemId);
   elem.style.backgroundColor=selected?'#ffffff':'#C4FA91';
   
}

document.onmouseup=function()
	{
		if(canDrg==true)
		{
			canDrg=false;
			
			if(objt[bdg_lastnearindex])
			{
			     var lastNearElem=document.getElementById(objt[bdg_lastnearindex].name);
				 if(lastNearElem.getAttribute('belong')=='left')
				 { 
				    selectElement(objt[bdg_lastnearindex].name,false);
				 }
			 }//note: if onmousedown no move, bgd_nearindex is undefine.
			if(objt[bdg_nearindex]&&objt[bdg_nearindex].name!=bdg_curmodid)
			{
			    var destElem=document.getElementById(objt[bdg_nearindex].name);
				if(destElem.getAttribute('belong')=='left')
				{
				   replaceItem(document.getElementById(bdg_curmodid),destElem);
				}
			}
					
			if($('rightDialogTbl').rows.length==4)
			{
			   $('nobandTr').style.display='block';
			}		
			if(document.all&&thiso!=null)
			{
				thiso.releaseCapture()
			}
			document.getElementById("fordrag").style.display="none";
			bdg_nearindex="-1";
			bdg_lastnearindex="-1";
			thiso=null;
			document.onmousedown=function()
			{
			return true
			};
			document.body.onselectstart=function()
			{
			return true
			}
		}
		//alert(debug);

	};
function modobj(modname,x,y,owidth,oheight)
	{
		this.name=modname;
		this.x=x;
		this.y=y;
		this.width=owidth;
		this.height=oheight
	};
var objt=new Array();
function getall()
	{
		var div2=document.getElementsByTagName("div");
		var j=0;
		objt.length=0;
		for(var i=0;i<div2.length;i++)
			{
				if(div2[i].getAttribute("rel")=="drag")
				{
					var o=div2[i];
					var t=o.offsetTop;
					var l=o.offsetLeft;
					while(o=o.offsetParent)
					{
						t+=o.offsetTop;
						l+=o.offsetLeft
					}
					objt[j]=new modobj(div2[i].id,l,t,div2[i].offsetWidth,div2[i].offsetHeight);
					j++
				}
			}
	};
function getmin(e)
	{
			var distance=new Array();
	 		for(i=0;i<objt.length;i++)
				{
					distance[i]=Math.pow((bdg_eventX-bdg_offsetX-objt[i].x),2)+Math.pow((bdg_eventY-bdg_offsetY-objt[i].y),2)
				}
			if(distance.length<=1)
				{
					return 0
				}else
				{
					var mindis=distance[0];
					var minid=0;
					if(distance[0]==null)
					{
						mindis=distance[1];
						minid=1
					}
					for(i=1;i<distance.length;i++)
					{
						if(distance[i]!=null&&mindis>distance[i])
						{
							mindis=distance[i];
							minid=i
						}
					}
				}
				return minid
		};
function addnewmod(id,beforeid)
		{
		    if($('rightDialogTbl').rows.length==4)
			{
			   $('nobandTr').style.display='none';
			}
		
			var elem=document.createElement("div");
			elem.setAttribute("id",id);
			elem.setAttribute("belong","right");
			elem.setAttribute("rel","drag");
			elem.innerHTML=document.getElementById("dragbak").innerHTML;
		    
			var recentDiv= document.getElementById(beforeid);
			var tableSection=recentDiv.parentNode.parentNode.parentNode;
			var row=tableSection.insertRow(0);
			var tblCell=row.insertCell(0);
			tblCell.align='left';
			tblCell.bgColor='#C4FA91';
			tblCell.width='100%'
			tblCell.style.paddingLeft='5px'
			tblCell.appendChild(elem);
			
			
			var tblCell1=row.insertCell(1);
			tblCell1.width='25';
			tblCell1.height='25';
			tblCell1.bgColor='#C4FA91';
			
			var tblCell2=row.insertCell(2);
			
			
            tableSection.insertBefore(row,recentDiv.parentNode.parentNode)
            var lineRow=tableSection.insertRow(0);
			var cell=lineRow.insertCell(0);
			cell.height=1;
			cell.colspan=3;
			cell.bgColor='#7AA650';
			tableSection.insertBefore(lineRow,recentDiv.parentNode.parentNode);
		};
function delmod(id)
		{
			var curobj=document.getElementById(id);
			var tr=curobj.parentNode.parentNode;
			var lineTr=tr.nextSibling;
			while(lineTr.nodeType!=1)
			{
			   lineTr=lineTr.nextSibling;
			}
			tr.parentNode.removeChild(tr);
			lineTr.parentNode.removeChild(lineTr);
		};
